|
|
|
|
|
|
|
General file errors occur because of invalid data file information such as a bad filename, data path, or device name. Usually the user can fix these errors, and the program can continue from the point of failure. The basic approach to handling general file errors is to create an error handler that reports the problem to the user and asks for additional information to complete or retry the operation. |
|
|
|
|
|
|
|
|
In Listing 14.9, the error handler is called when the program attempts to open a control file called CONTROL.TXT. The error handler then prompts the user for the proper file location and continues processing. Start a new Standard EXE project (ERRTYPES.VBP) and add a command button to the form. Set its Caption property to Control and its Name property to cmdControl. Also, add a CommonDialog control to the project. Enter the code in Listing 14.9 into the cmdControl_Click event. |
|
|
|
|
|
|
|
|
Listing 14.9. Adding code to the cmdControl_Click event. |
|
|
|
|
|
|
|
|
Private Sub cmdControl_Click()
'
' show general file errors
'
On Error GoTo LocalErr
'
Dim strFile As String
Dim strMsg As String
Dim lngReturn As Long
'
strFile = "\control.txt"
'
Open strFile For Input As 1
MsgBox "Control File Opened"
GoTo LocalExit
'
LocalErr:
If Err.Number = 53 Then ' file not found?
strMsg = "Unable to Open CONTROL.TXT" & vbCrLf
strMsg = strMsg & "Select OK to locate CONTROL.TXT" & vbCrLf
strMsg = strMsg & "Select CANCEL to exit program."
'
lngReturn = MsgBox(strMsg, vbCritical + vbOKCancel, "cmdControl")
'
If lngReturn = vbOK Then
CommonDialog1.filename = strFile
CommonDialog1.DefaultExt = ".txt"
CommonDialog1.ShowOpen
Resume
Else
Unload Me
End If
Else |
|
|
|
|
|